package com.erp.mclass;

import com.erp.bean.Twostringbean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/11/011.
 * 作者：刘洋
 * 用于添加观察者的抽象类
 */
public abstract class Observable<T> {


    public final ArrayList<HashMap<String, T>> obserList = new ArrayList<HashMap<String,T>>();

    /* 添加观察者 包括名称及本生 */
    public void attachObserver(String obName, T ob) {
        if(obName == null || ob == null) throw new NullPointerException();
        synchronized(obserList) {
            HashMap<String, T> map = new HashMap<String, T>();
            map.put(obName, ob);
            int index = obserList.indexOf(map);
            if(index == -1) {
                obserList.add(map);
            }
        }
    }
    /* 删除观察者 */
    public void detachObserver(String obName) {
        if(obName == null) throw new NullPointerException();
        synchronized(obserList){
            Iterator<HashMap<String, T>> iteratorMap = obserList.iterator();
            while(iteratorMap.hasNext()) {
                Iterator<Map.Entry<String, T>> iterator = iteratorMap.next().entrySet().iterator();
                while(iterator.hasNext()){
                    if(iterator.next().getKey().equals(obName)){
                        iteratorMap.remove();
                        break;
                    }
                }
            }
        }
    }

    /** detach all observers */
    public void detachObservers() {
        synchronized(obserList) {
            obserList.removeAll(obserList);
        }
    }

    /** Ruturn the size of observers */
    public int countObservers() {
        synchronized(obserList) {
            return obserList.size();
        }
    }

    public abstract void notifyObservers();

    public abstract void notifyObserver(String obserName, boolean flag, String s, int select, ArrayList<Twostringbean> searchParams, Twostringbean time);
}
